package com.tencent50.easy;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 相交链表
 * @date 2022/7/24 16:04
 */
public class Demo13 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode c=headA;
        ListNode d=headB;
        if(headA==null||headB==null){
            return null;
        }
        int count1=0,count2=0;
        while (headA!=null){
            count1++;
            headA=headA.next;
        }
        while (headB!=null){
            count2++;
            headB=headB.next;
        }
        if(count1>=count2){
            for (int i = 0; i < count1-count2; i++) {
                c=c.next;
            }
        }else {
            for (int i = 0; i < count2-count1; i++) {
                d=d.next;
            }
        }
        while (c!=null&&d!=null){
            if(c==d){
                return c;
            }
            c=c.next;
            d=d.next;
        }
        return null;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
